package de.htw_saarland.webservices.uddi;

import java.util.List;

import javax.jws.WebService;
import javax.xml.ws.Endpoint;

import org.uddi.api_v3.BindingDetail;
import org.uddi.api_v3.BusinessDetail;
import org.uddi.api_v3.BusinessList;
import org.uddi.api_v3.FindBinding;
import org.uddi.api_v3.FindBusiness;
import org.uddi.api_v3.FindRelatedBusinesses;
import org.uddi.api_v3.FindService;
import org.uddi.api_v3.FindTModel;
import org.uddi.api_v3.GetBindingDetail;
import org.uddi.api_v3.GetBusinessDetail;
import org.uddi.api_v3.GetOperationalInfo;
import org.uddi.api_v3.GetServiceDetail;
import org.uddi.api_v3.GetTModelDetail;
import org.uddi.api_v3.OperationalInfos;
import org.uddi.api_v3.RelatedBusinessesList;
import org.uddi.api_v3.ServiceDetail;
import org.uddi.api_v3.ServiceList;
import org.uddi.api_v3.TModelDetail;
import org.uddi.api_v3.TModelList;
import org.uddi.v3_service.DispositionReportFaultMessage;
import org.uddi.v3_service.UDDIInquiryPortType;

@WebService(name="UDDIInquiryService")
public class UDDIInquiryNode implements UDDIInquiryPortType {
	
	public UDDIInquiryNode() {
		super();
	}
	
	@Override
	public BindingDetail findBinding(FindBinding body)
			throws DispositionReportFaultMessage {

		checkAuthToken(body.getAuthInfo());
		/*
		UDDIPersistenceNode port = m_persistenceService.getUDDIPersistenceNodePort();
		
		List<String> qualifiers = body.getFindQualifiers().getFindQualifier();
		
		for (String qualifier : qualifiers) {
			port.findBinding(qualifier);
		}
		*/
		//TODO: Return a valid object
		return new BindingDetail();
	}

	@Override
	public BusinessList findBusiness(FindBusiness body)
			throws DispositionReportFaultMessage {
		
		checkAuthToken(body.getAuthInfo());
		/*
		UDDIPersistenceNode port = m_persistenceService.getUDDIPersistenceNodePort();
		
		List<String> qualifiers = body.getFindQualifiers().getFindQualifier();
		
		for (String qualifier : qualifiers) {
			port.findBusiness(qualifier);
		}
		*/
		//TODO: Return a valid object
		return new BusinessList();
	}

	@Override
	public RelatedBusinessesList findRelatedBusinesses(
			FindRelatedBusinesses body) throws DispositionReportFaultMessage {
		
		checkAuthToken(body.getAuthInfo());
		/*
		UDDIPersistenceNode port = m_persistenceService.getUDDIPersistenceNodePort();
		
		port.findRelatedBusinesses(body.getBusinessKey());
		*/
		//TODO: Return a valid object
		return new RelatedBusinessesList();
	}

	@Override
	public ServiceList findService(FindService body)
			throws DispositionReportFaultMessage {
		
		checkAuthToken(body.getAuthInfo());
		/*
		UDDIPersistenceNode port = m_persistenceService.getUDDIPersistenceNodePort();
		
		List<String> qualifiers = body.getFindQualifiers().getFindQualifier();
		
		for (String qualifier : qualifiers) {
			port.findService(qualifier);
		}
		*/
		//TODO: Return a valid object
		return new ServiceList();
	}

	@Override
	public TModelList findTModel(FindTModel body)
			throws DispositionReportFaultMessage {
		
		checkAuthToken(body.getAuthInfo());
		/*
		UDDIPersistenceNode port = m_persistenceService.getUDDIPersistenceNodePort();
		
		List<String> qualifiers = body.getFindQualifiers().getFindQualifier();
		
		for (String qualifier : qualifiers) {
			port.findTModel(qualifier);
		}
		*/
		//TODO: Return a valid object
		return new TModelList();
	}

	@Override
	public BindingDetail getBindingDetail(GetBindingDetail body)
			throws DispositionReportFaultMessage {

		checkAuthToken(body.getAuthInfo());
		/*
		UDDIPersistenceNode port = m_persistenceService.getUDDIPersistenceNodePort();
		
		List<String> keys = body.getBindingKey();
		
		for (String key : keys) {
			port.getBindingDetail(key);
		}
		*/		
		//TODO: Return a valid object
		return new BindingDetail();
	}

	@Override
	public BusinessDetail getBusinessDetail(GetBusinessDetail body)
			throws DispositionReportFaultMessage {
		
		checkAuthToken(body.getAuthInfo());
		/*
		UDDIPersistenceNode port = m_persistenceService.getUDDIPersistenceNodePort();
		
		List<String> keys = body.getBusinessKey();
		
		for (String key : keys) {
			port.getBusinessDetail(key);
		}
		*/
		//TODO: Return a valid object
		return new BusinessDetail();
	}

	@Override
	public OperationalInfos getOperationalInfo(GetOperationalInfo body)
			throws DispositionReportFaultMessage {
		
		checkAuthToken(body.getAuthInfo());
		/*
		UDDIPersistenceNode port = m_persistenceService.getUDDIPersistenceNodePort();
		
		List<String> qualifiers = body.getEntityKey();
		
		for (String qualifier : qualifiers) {
			port.getOperationalInfos(qualifier);
		}
		*/
		//TODO: Return a valid object
		return new OperationalInfos();
	}

	@Override
	public ServiceDetail getServiceDetail(GetServiceDetail body)
			throws DispositionReportFaultMessage {
		
		checkAuthToken(body.getAuthInfo());
		/*
		UDDIPersistenceNode port = m_persistenceService.getUDDIPersistenceNodePort();
		
		List<String> keys = body.getServiceKey();
		
		for (String key : keys) {
			port.getServiceDetail(key);
		}
		*/
		//TODO: Return a valid object
		return new ServiceDetail();
	}

	@Override
	public TModelDetail getTModelDetail(GetTModelDetail body)
			throws DispositionReportFaultMessage {
		
		checkAuthToken(body.getAuthInfo());
		/*
		UDDIPersistenceNode port = m_persistenceService.getUDDIPersistenceNodePort();
		
		List<String> keys = body.getTModelKey();
		
		for (String key : keys) {
			port.getTModelDetail(key);
		}
		*/
		//TODO: Return a valid object
		return new TModelDetail();
	}

	private void checkAuthToken(String token) 
	throws DispositionReportFaultMessage {
	
		if ((token == null) || (token.isEmpty())) {
			throw new DispositionReportFaultMessage("Authentication token invalid", null);
		}
	}	
	
	/**
	 * Create web service for testing purposes. Only works with Java6.
	 */
	public static void main(String[] args) {
		UDDIInquiryNode node = new UDDIInquiryNode();
		Endpoint.publish("http://localhost:8080/uddiinquirynode", node);

	}
	
}
